package com.newtype.meeting.client.widget.shared;

import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelIconProvider;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.CheckChangedEvent;
import com.extjs.gxt.ui.client.event.CheckChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.store.TreeStore;
import com.extjs.gxt.ui.client.util.Format;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.ListView;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.StoreFilterField;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

public class OrganizationSelectWindow extends Window {
	private TreeStore<ModelData> store = new TreeStore<ModelData>();
	private ListStore<ModelData> listStore = new ListStore<ModelData>();
	private ListView<ModelData> listView;

	public OrganizationSelectWindow(Map<String, Field<String>> personnel) {
		setSize(450, 500);
		setModal(true);
		setLayout(new BorderLayout());

		createWest();
		createCenter();

		addButton(getConfirmButton(personnel));
		addButton(getCancelButton());
	}

	/**
	 * 取消按钮
	 * @return
	 */
	private Button getCancelButton() {
		Button button = new Button("取消");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				hide();
			}
		});
		return button;
	}

	/**
	 * 确定选取按钮
	 * @return
	 */
	private Button getConfirmButton(final Map<String, Field<String>> personnel) {
		Button button = new Button("确定选取");
		button.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				StringBuffer sbAccountID = new StringBuffer();//封装人员ID
				StringBuffer sbFullName = new StringBuffer();//封装人员姓名
				for (ModelData model : listStore.getModels()) {
					sbAccountID.append(", " + (String) model.get("accountID"));
					sbFullName.append(", " + (String) model.get("name"));
				}
				String accountID = sbAccountID.toString();
				String fullName = sbFullName.toString();
				if (accountID.length() > 1)
					accountID = accountID.substring(2);
				if (fullName.length() > 1)
					fullName = fullName.substring(2);
				//赋值
				personnel.get("accountID").setValue(
						Format.ellipse(accountID, 100));
				personnel.get("fullName").setValue(
						Format.ellipse(fullName, 100));
				hide();
			}
		});
		return button;
	}

	private void createWest() {
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST,
				225, 200, 250);
		westData.setMargins(new Margins(5, 0, 5, 5));
		VerticalPanel westPanel = new VerticalPanel();
		westPanel.setSpacing(8);
		westPanel.setBorders(true);
		westPanel.setLayout(new FormLayout());
		westPanel.setScrollMode(Scroll.AUTO);
		westPanel.setStyleAttribute("backgroundColor", "white");

		final TreePanel<ModelData> tree = new TreePanel<ModelData>(store);
		tree.setBorders(false);
		tree.setDisplayProperty("name");
		tree.setIconProvider(new TreeModelIconProvider());//显示图标
		tree.setCheckable(true);
		tree.setAutoLoad(true);
		tree.setCheckStyle(CheckCascade.CHILDREN);

		tree.addCheckListener(new CheckChangedListener<ModelData>() {
			@Override
			public void checkChanged(CheckChangedEvent<ModelData> event) {
				listStore.removeAll();
				List<ModelData> models = event.getCheckedSelection();
				for (int i = 0; i < models.size(); i++) {
					if (models.get(i).get("accountID") != null
							&& !"".equals(models.get(i).get("accountID"))) {
						listStore.add(models.get(i));
					}
				}
			}
		});

		StoreFilterField<ModelData> filter = new StoreFilterField<ModelData>() {
			@Override
			protected boolean doSelect(Store<ModelData> store,
					ModelData parent, ModelData record, String property,
					String filter) {
				String name = record.get("name");
				name = name.toLowerCase();
				if (name.indexOf(filter.toLowerCase()) != -1) {
					return true;
				}
				return false;
			}
		};
		filter.bind(store);

		ButtonBar buttonBar = new ButtonBar();
		buttonBar.add(new Button("Expand All",
				new SelectionListener<ButtonEvent>() {
					public void componentSelected(ButtonEvent ce) {
						tree.expandAll();
					}
				}));
		buttonBar.add(new Button("Collapse All",
				new SelectionListener<ButtonEvent>() {
					public void componentSelected(ButtonEvent ce) {
						tree.collapseAll();
					}
				}));
		//westPanel.add(new Html("<span class='text'>请输入查询内容：</span>"));
		westPanel.add(filter);
		westPanel.add(buttonBar);
		westPanel.add(tree);

		add(westPanel, westData);
	}

	private void createCenter() {
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5));
		ContentPanel centerPanel = new ContentPanel();
		centerPanel.setHeading("已选人员");

		listView = new ListView<ModelData>() {
			@Override
			protected ModelData prepareData(ModelData model) {
				String fullName = model.get("name");
				String deptName = model.get("deptName");
				String name = fullName + " [" + deptName + "]";
				model.set("shortName", Format.ellipse(name, 15));
				return model;
			}
		};
		listView.setStore(listStore);
		listView.setDisplayProperty("shortName");
		listView.setBorders(false);

		centerPanel.add(listView);
		add(centerPanel, centerData);
	}

	public TreeStore<ModelData> getStore() {
		return store;
	}

	/**
	 * 图标显示
	 * @author 汪恭泽
	 *
	 */
	public class TreeModelIconProvider implements ModelIconProvider<ModelData> {
		@Override
		public AbstractImagePrototype getIcon(ModelData model) {
			if (model.get("accountID") != null) {
				return IconHelper.createPath(GWT.getHostPageBaseURL()
						+ "images/icons/member.gif");
			} else {
				return IconHelper.createPath(GWT.getHostPageBaseURL()
						+ "images/icons/dept.gif");
			}
		}
	}
}